﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class LoginService
{
	// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
	// To create an operation that returns XML,
	//     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
	//     and include the following line in the operation body:
	//         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
	[OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
	public string AdminLogin(string userName,string password,int person)
	{
		// Add your operation implementation here
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "ADM_sCheckLogin";

        cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar)).Value = userName;
        cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar)).Value = password;
        cmd.Parameters.Add(new SqlParameter("@Role", SqlDbType.Int)).Value = person;
        

        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(ds);
        con.Close();

        return "{Message:'" + ds.Tables[0].Rows[0]["message"] + "',Person :'" + ds.Tables[0].Rows[0]["Person"] + "',PersonName :'" + ds.Tables[0].Rows[0]["PersonName"] + "'}";
	}



    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    public string UserLogin(string username, string password)
    {
        // Add your operation implementation here
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pCheckUserLogin";

        cmd.Parameters.Add(new SqlParameter("@Username", SqlDbType.VarChar)).Value = username;
        cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar)).Value = password;      


        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(ds);
        con.Close();

       // return "{Message:'" + ds.Tables[0].Rows[0]["message"] + "',Person :'" + ds.Tables[0].Rows[0]["Person"] + "',PersonName :'" + ds.Tables[0].Rows[0]["PersonName"] + "'}";
        return "{Message:'" + ds.Tables[0].Rows[0]["message"] + "',User :'" + ds.Tables[0].Rows[0]["user"] + "'}";
    }


    
    
    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string changepwd(string Password, string NewPassword)
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "ADM_sChangePassword";

        cmd.Parameters.Add("@currentPassword", SqlDbType.VarChar).Value = Password;
        cmd.Parameters.Add("@newPassword", SqlDbType.VarChar).Value = NewPassword;
        
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();
        return "{Message: '" + ds.Tables[0].Rows[0]["Message"] + "'}";
    }


    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string changeSalesuserPwd(string Password, string NewPassword)
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "ADM_sChangePassword";

        cmd.Parameters.Add("@currentPassword", SqlDbType.VarChar).Value = Password;
        cmd.Parameters.Add("@newPassword", SqlDbType.VarChar).Value = NewPassword;

        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();
        return "{Message: '" + ds.Tables[0].Rows[0]["Message"] + "'}";
    }
	

}
